package com.webwork.lims.transport.dao.impl;

import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

import org.springframework.stereotype.Repository;

import com.webwork.lims.common.dao.impl.SimpleDao;
import com.webwork.lims.common.dataobject.CtrlerAndTime;
import com.webwork.lims.transport.dao.TransDao;
import com.webwork.lims.transport.dataobject.TransOrder;
import com.webwork.lims.transport.dataobject.TransStatus;
import com.webwork.lims.util.ColumnLimit;
import com.webwork.lims.util.DBSimpleSQL;
import com.webwork.lims.util.StrManager;

@Repository
public class TransDaoImpl extends SimpleDao implements TransDao {

	public TransDaoImpl() {
		super();
		this.dbTable = "t_transport";
		dbColumns.clear();
		dbColumns.put("transId", "TRANS_ID");
		dbColumns.put("carNum", "CAR_NUM");
		dbColumns.put("wayLine", "WAY_LINE");
		dbColumns.put("hours", "HOURS");
		dbColumns.put("arrive", "ARRIVE");
		dbColumns.put("predict", "PREDICT");
		dbColumns.put("status", "STATUS");
		dbColumns.put("createPerson", "CREATE_PERSON");
		dbColumns.put("createTime", "CREATE_TIME");
		dbColumns.put("deliveryPerson", "DELIVERY_PERSON");
		dbColumns.put("deliveryTime", "DELIVERY_TIME");
		dbColumns.put("modifyPerson", "MODIFY_PERSON");
		dbColumns.put("modifyTime", "MODIFY_TIME");
		dbColumns.put("completePerson", "COMPLETE_PERSON");
		dbColumns.put("completeTime", "COMPLETE_TIME");
	}
	
	@Override
	public ColumnLimit condition(Object object) {
		if (!(object instanceof TransOrder))
			return new ColumnLimit();
		TransOrder o = (TransOrder) object;
		ColumnLimit ret = new ColumnLimit();
		if (!StrManager.isEmpty(o.getTransId())) {
			ret.add(dbColumns.get("transId") + " = ");
		}
		if (o.getStatus() != null && o.getStatus() != TransStatus.NOSET) {
			ret.add(dbColumns.get("status") + " = ");
		}
		if (!StrManager.isEmpty(o.getWayLine())) {
			ret.add(dbColumns.get("wayLine") + " like ");
		}
		if (o.getPredict() != 0) {
			ret.add(dbColumns.get("predict") + " = ");
		}
		return ret;
	}

	@Override
	public Object[] statement(Object object) {
		TransOrder o = (TransOrder) object;
		List<Object> ret = new ArrayList<Object>();
		if (!StrManager.isEmpty(o.getTransId())) {
			ret.add(o.getTransId());
		}
		if (o.getStatus() != null && o.getStatus() != TransStatus.NOSET) {
			ret.add(o.getStatus().getCode());
		}
		if (!StrManager.isEmpty(o.getWayLine())) {
			ret.add("%" + o.getWayLine() + "%");
		}
		if (o.getPredict() != 0) {
			ret.add(o.getPredict());
		}
		return ret.toArray();
	}

	@Override
	public List<Object> pushObject(Object object) {
		TransOrder o = (TransOrder) object;
		List<Object> objs = new ArrayList<Object>();
		objs.add(o.getTransId());
		objs.add(o.getCarNum());
		objs.add(o.getWayLine());
		objs.add(o.getHours());
		objs.add(o.getArrive());
		objs.add(o.getPredict());
		objs.add(o.getStatus().getCode());
		objs.add(o.getCreate() == null ? null : o.getCreate().getController());
		objs.add(o.getCreate() == null ? new Date(0) : o.getCreate().getTime());
		objs.add(o.getDelivery() == null ? null : o.getDelivery().getController());
		objs.add(o.getDelivery() == null ? new Date(0) : o.getDelivery().getTime());
		objs.add(o.getModify() == null ? null : o.getModify().getController());
		objs.add(o.getModify() == null ? new Date(0) : o.getModify().getTime());
		objs.add(o.getComplete() == null ? null : o.getComplete().getController());
		objs.add(o.getComplete() == null ? new Date(0) : o.getComplete().getTime());
		return objs;
	}

	@Override
	public Object catchObject(Map<String, Object> map) {
		TransOrder ret = new TransOrder();
		ret.setTransId((String) map.get(dbColumns.get("transId")));
		ret.setCarNum((String) map.get(dbColumns.get("carNum")));
		ret.setWayLine((String) map.get(dbColumns.get("wayLine")));
		ret.setHours((Integer) map.get(dbColumns.get("hours")));
		ret.setArrive((String) map.get(dbColumns.get("arrive")));
		ret.setPredict((Integer) map.get(dbColumns.get("predict")));
		ret.setStatus((Integer) map.get(dbColumns.get("status")));
		ret.setCreate(new CtrlerAndTime((String) map.get(dbColumns.get("createPerson")), (Timestamp) map.get(dbColumns.get("createTime"))));
		ret.setDelivery(new CtrlerAndTime((String) map.get(dbColumns.get("deliveryPerson")), (Timestamp) map.get(dbColumns.get("deliveryTime"))));
		ret.setModify(new CtrlerAndTime((String) map.get(dbColumns.get("modifyPerson")), (Timestamp) map.get(dbColumns.get("modifyTime"))));
		ret.setComplete(new CtrlerAndTime((String) map.get(dbColumns.get("completePerson")), (Timestamp) map.get(dbColumns.get("completeTime"))));
		return ret;
	}
	
	@Override
	public void insert(TransOrder o) {
		String sql = DBSimpleSQL.insert(dbTable, dbColumns.size());
		this.getJdbcTemplate().update(sql, this.pushObject(o).toArray());
	}
	
	@Override
	public void update(TransOrder o) {
		ColumnLimit limit = new ColumnLimit();
		limit.add(dbColumns.get("transId") + " = ");
		String sql = DBSimpleSQL.update(dbTable, dbColumns, limit);
		List<Object> objs = this.pushObject(o);
		objs.add(o.getTransId());
		this.getJdbcTemplate().update(sql, objs.toArray());
	}

	@Override
	public void delete(String transId) {
		ColumnLimit limit = new ColumnLimit();
		limit.add(dbColumns.get("transId") + " = ");
		String sql = DBSimpleSQL.del(dbTable, limit);
		this.getJdbcTemplate().update(sql, transId);
	}

	@Override
	public List<TransOrder> query(TransOrder o, int page, int size) {
		List<TransOrder> ret = new ArrayList<TransOrder>();
		String sql = DBSimpleSQL.selectAll(dbTable, this.condition(o), page, size);
		List<Map<String, Object>> maps = this.getJdbcTemplate().queryForList(sql, this.statement(o));
		for (Map<String, Object> map : maps) {
			ret.add((TransOrder) this.catchObject(map));
		}
		return ret;
	}

	@Override
	public int count(TransOrder o) {
		String sql = DBSimpleSQL.count(dbTable, this.condition(o));
		return this.getJdbcTemplate().queryForObject(sql, this.statement(o), Integer.class);
	}
	
}
